#pragma once
#include "../Comm/Httplib.hpp"
#include "../Comm/Log.hpp"
#include "../Comm/Util.hpp"
#include <vector>
#include <unordered_map>
#include <assert.h>
#include <stdlib.h>
#include "include/mysql.h"

namespace ns_model
{
    using namespace httplib;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number;//编号
        std::string title;//标题
        std::string star;//难度
        std::string desc;//描述
        std::string header;//预设代码
        std::string tail;//测试用例
        int cpu_limit;//时间要求
        int mem_limit;//空间要求
    };

    const std::string mysql_table = "oj_questions";

    const std::string host = "127.0.0.1";
    const std::string user = "ojclient";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;

    class Model
    {
    public:
        Model()
        {
        }
        ~Model(){}
        bool QueryMysql(const std::string& sql, std::vector<Question>* out)
        {
            // 创建
            MYSQL *my = mysql_init(nullptr);
            // 连接 
            if(nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败" << "\n";
                std::cout << mysql_error(my) << std::endl;
                return false;
            }

            // 设置编码格式
            mysql_set_character_set(my, "utf8");

            LOG(INFO) << "连接数据库成功" << "\n";

            // 执行
            if(0 != mysql_query(my, sql.c_str()))
            {
                LOG(ERROR) << "执行语句失败" << "\n";
                return false;
            }
            // 提取结果
            MYSQL_RES *res = mysql_store_result(my);

            // 分析结果
            int rows = mysql_num_rows(res);
            int cols = mysql_num_fields(res);

            Question q;
            for(int i=0; i<rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = std::atoi(row[6]);
                q.mem_limit = std::atoi(row[7]);

                out->push_back(q);
            }

            // 释放结果
            free(res);
            // 关闭
            mysql_close(my);
            return true;
        }
        bool getAllQuestions(std::vector<Question>* qs)
        {
            //获取所有题目
            std::string sql = "select * from ";
            sql += mysql_table;
            return QueryMysql(sql, qs);
        }
        bool getOneQuestion(const std::string& number, Question* q)
        {
            //获取指定题目
            bool ret = false;
            std::string sql = "select * from ";
            sql += mysql_table;
            sql += " where number=";
            sql += number;
            std::vector<Question> res;
            if(QueryMysql(sql, &res))
            {
                *q = res[0];
                ret = true;
            }
            return ret;
        }
    private:
        std::unordered_map<std::string, Question> questions;
    };

}
